﻿using System.Text;
using RapChieuPhim.DTO;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using RapChieuPhim.BUS;
using System;

namespace RapChieuPhim.DAO
{
    class CTPhongDAO
    {
        public CTPhongChieu GetByID(int rap, int phong, int phim, DateTime ngay)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@MaRap", rap);
            param[1] = new SqlParameter("@MaPhong", phong);
            param[2] = new SqlParameter("@MaPhim", phim);
            param[3] = new SqlParameter("@NgayChieu", ngay);
            DataTable dt = cn.SqlExcuteQuery("LayLichChieuByID", param);
            CTPhongChieu CTPhong = new CTPhongChieu();

            if (dt.Rows.Count != 0)
            {
                CTPhong.Rap = RapBUS.GetByID(int.Parse(dt.Rows[0]["MaRap"].ToString()));
                CTPhong.Phong = PhongChieuBUS.GetByID(int.Parse(dt.Rows[0]["MaPhong"].ToString()));
                CTPhong.Phim = PhimBUS.GetByID(int.Parse(dt.Rows[0]["MaPhim"].ToString()));
                CTPhong.NgayChieu = DateTime.Parse(dt.Rows[0]["NgayChieu"].ToString());
            }
            return CTPhong;
        }

        public void Insert(DateTime _NgayChieu, int _MaPhong, int _MaPhim, int _MaRap)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@MaPhong", _MaPhong);
            param[1] = new SqlParameter("@MaPhim", _MaPhim);
            param[2] = new SqlParameter("@NgayChieu", _NgayChieu);
            param[3] = new SqlParameter("@MaRap", _MaRap);
            cn.SqlExcuteNonQuery("ThemLichChieu", param);
        }

        public void Delete(int rap, int phim)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaRap", rap);
            param[1] = new SqlParameter("@MaPhim", phim);
            cn.SqlExcuteNonQuery("XoaLichChieu", param);
        }

        public void DeleteCuThe(int phong, int phim, DateTime ngay, int rap)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@MaPhong", phong);
            param[1] = new SqlParameter("@MaPhim", phim);
            param[2] = new SqlParameter("@NgayChieu", ngay);
            param[3] = new SqlParameter("@MaRap", rap);
            cn.SqlExcuteNonQuery("XoaLichChieuCuThe", param);
        }

        public IList GetAllbyMaRap(int _MaRap)
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            SqlParameter param = new SqlParameter("@MaRap", _MaRap);
            DataTable dt = cn.SqlExcuteQuery("LayAllLichChieu", param);

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    CTPhongChieu CTPhong = new CTPhongChieu();
                    CTPhong.Rap = RapBUS.GetByID(int.Parse(dr["MaRap"].ToString()));
                    CTPhong.Phim = PhimBUS.GetByID(int.Parse(dr["MaPhim"].ToString()));
                    CTPhong.Phong = PhongChieuBUS.GetByID(int.Parse(dr["MaPhong"].ToString()));
                    CTPhong.NgayChieu = DateTime.Parse(dr["NgayChieu"].ToString());
                    array.Add(CTPhong);
                }
            }
            return array;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayInfoLichChieu");

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    CTPhongChieu CTPhong = new CTPhongChieu();
                    CTPhong.Rap = RapBUS.GetByID(int.Parse(dr["MaRap"].ToString()));
                    CTPhong.Phim = PhimBUS.GetByID(int.Parse(dr["MaPhim"].ToString()));
                    CTPhong.Phong = PhongChieuBUS.GetByID(int.Parse(dr["MaPhong"].ToString()));
                    CTPhong.NgayChieu = DateTime.Parse(dr["NgayChieu"].ToString());
                    array.Add(CTPhong);
                }
            }
            return array;
        }

        public DataTable CheckLichTrung(int _MaPhong, int _MaPhim, DateTime _NgayChieu, int _MaRap)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@MaPhong", _MaPhong);
            param[1] = new SqlParameter("@MaPhim", _MaPhim);
            param[2] = new SqlParameter("@NgayChieu", _NgayChieu);
            param[3] = new SqlParameter("@MaRap", _MaRap);
            DataTable dt = cn.SqlExcuteQuery("CheckLichTrung", param);
            return dt;
        }
    }
}
